﻿using System.Linq.Expressions;

namespace Core.Extensions;

public static class QueryableExtensions
{
    /// <summary>
    /// 根据条件动态添加 Where 过滤条件
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    /// <param name="source">原始查询</param>
    /// <param name="condition">是否应用过滤</param>
    /// <param name="predicate">过滤表达式</param>
    /// <returns>IQueryable</returns>
    public static IQueryable<T> WhereIf<T>(
        this IQueryable<T> source,
        bool condition,
        Expression<Func<T, bool>> predicate)
    {
        return condition ? source.Where(predicate) : source;
    }
}
